<template>
  <div class="padding-left-right-20 padding-top-20">
    <el-row type="flex" justify="end" style="margin-bottom: 15px">
      <el-form @submit.native.prevent style="flex: 1; margin-right: 10px">
        <el-input placeholder="请输入人员结构名称" v-model.trim="searchValue" @keyup.enter.native="selectList(1)" size="mini" />
      </el-form>
      <el-button type="primary" size="mini" @click="selectList(1)">查询</el-button>
      <el-button type="primary" size="mini" @click="addBtn">新增结构</el-button>
    </el-row>
    <el-row>
      <el-table :data="list" size="medium" @row-click="management" :border="false" :show-header="false">
        <el-table-column label="流程名称" align="left" prop="structureName" />
        <el-table-column label="更改时间" width="180" align="right">
          <template slot-scope="scope">
            <span class="common-table-item">更改时间：{{ scope.row.updateTime | formatDate }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100" align="right">
          <template slot-scope="scope">
            <el-button type="text el-icon-delete" @click.stop="deleteItem(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-row>

    <el-pagination class="t-right margin-top-10" prev-text="上一页" next-text="下一页" layout="prev, pager, next" :total="queryParams.total" :current-page.sync="queryParams.currentPage" :page-size="queryParams.pageSize" @current-change="selectList" />

    <el-dialog :title="dialog.model.primaryKey ? '编辑结构' : '新增结构'" :visible.sync="dialog.visible" width="50%" :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false">
      <el-form ref="dialogForm" :model="dialog.model" :rules="dialog.rules" label-width="100px" size="small" label-position="right">
        <el-row>
          <el-col>
            <el-form-item label="结构名称" prop="structureName">
              <el-input maxlength="50" autocomplete="off" clearable v-model.trim="dialog.model.structureName" placeholder="请输入结构名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="结构简称">
              <el-input maxlength="50" autocomplete="off" clearable v-model.trim="dialog.model.structureShortName" placeholder="请输入结构简称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col>
            <el-form-item label="组织介绍">
              <el-input type="textarea" :autosize="{ minRows: 3, maxRows: 6 }" placeholder="请输入组织介绍" v-model="dialog.model.description" maxlength="140" show-word-limit></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="addBtn">取 消</el-button>
        <el-button size="small" type="primary" @click="handleSave"> 确 定 </el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import { structureList, structureDelete, structureCreate, structureUpdate } from '../../api/structure';

  export default {
    data() {
      return {
        list: [],
        queryParams: {
          total: 0,
          pageSize: 20,
          currentPage: 1,
          structureType: 2,
          structureName: null,
        },
        searchValue: '',
        dialog: {
          visible: false,
          type: '',
          model: {
            structureName: '',
            structureShortName: '',
            structureType: 2,
            description: '',
          },
          rules: {
            structureName: [{ required: true, message: '请输入结构名称', trigger: 'blur' }],
          },
        },
      };
    },
    mounted() {
      this.selectList(1);
    },
    methods: {
      async selectList(currentPage = 1) {
        // this.queryParams.currentPage = currentPage || this.queryParams.currentPage;
        this.queryParams.structureName = this.searchValue ? this.searchValue : null;
        const { rows, total } = await structureList(this.queryParams);
        this.list = rows;
        this.queryParams.total = total;
      },
      addBtn(type = 'create', model) {
        if (this.dialog.visible) {
          this.$refs.dialogForm.clearValidate();
        }
        this.dialog.visible = !this.dialog.visible;
        this.dialog.type = type;
        if (this.dialog.visible) {
          if (model) {
            this.dialog.model = {
              ...this.dialog.model,
              ...model,
            };
          } else {
            this.dialog.model = {
              structureName: '',
              structureShortName: '',
              structureType: 2,
              description: '',
            };
          }
        }
      },
      async handleSave() {
        await this.$refs.dialogForm.validate();
        this.dialog.model.primaryKey ? await structureUpdate(this.dialog.model) : await structureCreate(this.dialog.model);
        this.$message.success('保存成功！');
        this.dialog.visible = false;
        this.dialog.model.primaryKey ? this.selectList(this.queryParams.currentPage) : this.selectList(1);
      },
      management(item) {
        let pathUrl = {
          name: 'person-structure-detail',
          query: {
            primaryKey: item.primaryKey,
          },
        };
        let url = this.$router.resolve(pathUrl);
        window.open(url.href, '_blank');
      },
      //删除
      deleteItem(item) {
        this.$confirm('确认删除?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(async () => {
            let res = await structureDelete(item.primaryKey);
            this.$message.success('删除成功!');
            setTimeout(() => {
              this.selectList(this.queryParams.currentPage);
            }, 500);
          })
          .catch(() => {});
      },
      pageChange(size) {
        this.queryParams.currentPage = size;
        this.selectList('more');
      },
    },
  };
</script>
